package com.zhangye.test.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ScanCount;
import com.zhangye.test.service.AdminService;
import com.zhangye.test.service.InfomationService;
import com.zhangye.test.service.QiniuService;
import com.zhangye.test.service.SafeSessionService;
import com.zhangye.test.service.ScanCountService;


/**
 * 个人信息表
 * 作者： 张叶
 * 创建时间：2018年6月22日  下午3:53:30
 */
@Controller
@RequestMapping("api/admin")
public class AdminApi {

	@Autowired
	private SafeSessionService safeSessionService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private InfomationService infomationService;
	@Autowired
	private QiniuService qiniuService;
	@Autowired
	private ScanCountService scanCountService;


	/**
	 * 数据管理
	 * 作者： 张叶
	 * 创建时间：2018年8月15日  下午3:56:09
	 */
	@ResponseBody
	@RequestMapping(value = "/adminData",method = RequestMethod.GET)
	public ApiResult adminData(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			result.data.put("restNumber", admin.getCounts());
			String where = " and admin_id = " + admin.getId() + " and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(created_at)";
			List<ScanCount> list = scanCountService.selectByWhere(where);
			result.data.put("weekendNumber", list.size());
//			where = " and admin_id = " + admin.getId() + " and DATE_FORMAT(created_at,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')";
			where = " and admin_id = " + admin.getId() + " and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(created_at)";
			list = scanCountService.selectByWhere(where);
			result.data.put("monthNumber", list.size());
			result.msg.status = 0;
			result.msg.desc = "查询成功";
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}


	/**
	 * 修改个人信息
	 * 作者： 张叶
	 * 创建时间：2018年7月23日  下午2:35:48
	 */
	@ResponseBody
	@RequestMapping(value = "/updateInfo",method = RequestMethod.POST)
	public ApiResult updateInfo(HttpServletRequest request,String username,
			String infomation,int sex,String birthday){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(username != null && !"".equals(username)){
				String where = " and username = '" + username + "' and id != " + admin.getId();
				List<Admin> list = adminService.selectWhere(where);
				if(list.size() == 0){
					admin.setUsername(username);
				}else{
					result.msg.status = 3;
					result.msg.desc = "用户名重复，请重新输入";
					return result;
				}
			}
			if(infomation != null && !"".equals(infomation)){
				admin.setInfomation(infomation);
			}
			if(birthday != null && !"".equals(birthday)){
				try {
				    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					admin.setBirthday(sdf.parse(birthday));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			admin.setSex(sex);
			int i = adminService.updateAdmin(admin, 0);
			if(i > 0){
				result.msg.status = 0;
				result.msg.desc = "修改成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "系统错误，修改失败";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}


	/**
	 * 获取个人信息
	 * 作者： 张叶
	 * 创建时间：2018年6月22日  下午3:54:02
	 */
	@ResponseBody
	@RequestMapping(value = "/getInfo",method = RequestMethod.GET)
	public ApiResult getInfo(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			admin = adminService.selectInfo(admin.getId());
			if(admin != null && !"".equals(admin)){
				if(admin.getBirthday() == null){
					try {
						SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
						admin.setBirthday(dateformat.parse("2000-01-01"));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				result.data.put("admin", admin);
				result.msg.status = 0;
				result.msg.desc = "查询成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "系统错误，查询失败";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	

	/**
	 * 上传头像
	 * 作者： 张叶
	 * 创建时间：2018年7月19日  上午11:04:15
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
	public ApiResult uploadImage(HttpServletRequest request,
			@RequestParam(value = "imageUrl", required = false) MultipartFile imageUrl){
		ApiResult result = new ApiResult();
		if(imageUrl != null && !"".equals(imageUrl)){
			Admin admin = safeSessionService.getCurrent(request);
			if(admin != null){
				try{
					String image = qiniuService.upload("file", imageUrl.getOriginalFilename(),imageUrl.getBytes());
					admin.setImage(image);
					int i = adminService.updateAdmin(admin, 0);
					if(i > 0){
						result.data.put("imageUrl", image);
						result.msg.status = 0;
						result.msg.desc = "上传成功";
					}else{
						result.msg.status = 1;
						result.msg.desc = "系统错误，上传失败";
					}
				}catch(Exception e){
					result.msg.status = 3;
					result.msg.desc = "上传图片失败";
				}
			}else{
				result.msg.status = 2;
				result.msg.desc = "当前登录已失效，请重新登录";
			}
		}
		return result;
	}
}
